import sys
import logging
from PyQt5.QtWidgets import QApplication
from utils.config_loader import load_config
from utils.file_utils import resource_path
from gui.main_window import MainWindow
from deploy_engine import DeploymentEngine

# 初始化配置
CONFIG = load_config(resource_path("resources/config.json"))

def setup_logging():
    """配置日志系统"""
    logging.basicConfig(
        level=CONFIG.get("log_level", "INFO"),
        format="%(asctime)s [%(levelname)s] %(message)s",
        handlers=[
            logging.FileHandler("navdeploy.log"),
            logging.StreamHandler()
        ]
    )

def main():
    # 初始化应用
    app = QApplication(sys.argv)
    
    try:
        # 加载核心引擎
        engine = DeploymentEngine(CONFIG)
        
        # 初始化主界面
        window = MainWindow(engine)
        window.setWindowTitle("领航部署专家")
        window.show()
        
        # 启动事件循环
        sys.exit(app.exec_())
        
    except Exception as e:
        logging.critical(f"致命错误: {str(e)}", exc_info=True)
        show_error_dialog("系统初始化失败，请检查日志文件")

if __name__ == "__main__":
    setup_logging()
    main()